<?php

class Student_ScaleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $course_id = $this->_getParam('id');
        $get_id = new Student_Model_DbTable_Course();
        $the_id = $get_id->getCourseDetails($course_id);

        $this_id = 0;
        foreach($the_id as $id){
            $this_id = "$id->course_name"." ($id->course_number)";
        }
         $this->view->title = "Grade Scale for <b>$this_id</b><br><br>";

       $this->view->headTitle($this->view->title, 'PREPEND');

       $scale = new Student_Model_DbTable_Scale();
       
       //$user_id = Zend_Auth::getInstance()->getIdentity()->id;
       $theScale = $scale->getCourseScale($course_id);
       
      // echo $theScale;
       $this->view->scale = $theScale;
    }

    public function addAction()
    {
        //$course_id = $this->_getParam('id');
       // $course = new Student_Model_DbTable_Course();
        $this->view->title = "<b>Add Grade Scale</b><br>";
        $this->view->headTitle($this->view->title, 'PREPEND');

        $form = new Student_Form_newScale();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData))
                        {
    			$grade = $form->getValue('grade');
                        $minimum = $form->getValue('minimum');
                        $maximum = $form->getValue('maximum');
                        $course_id = $this->_getParam('id');

                        //$valid = $form->getValues();
                       // print_r($valid);
                       // echo $course_id;

    			$add = new Student_Model_DbTable_Scale();
    			$add->addScale($course_id, $grade, $minimum, $maximum );
                        $this->view->message = "<u> Following scale was added:</u><br><br>";
                        $this->view->id = $course_id;
                        $this->view->grade = '<i>Grade:'.$grade.', ';
                        $this->view->minimum = '<i>Minimum:</i> '.$minimum.', ';
                        $this->view->maximum = '<i>Maximum:</i> '.$maximum.'<br><br></i>';



                        }
                else
                    {
    			$form->populate($formData);
                    }
    }
    }

    public function editAction()
    {
        $this->view->title = "Edit Grade Scale";
        $this->view->headTitle($this->view->title);
        $form = new Student_Form_NewScale();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        if ($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                        $id = $this->_getParam('id');
                        //echo $id;
                        $grade = $form->getValue('grade');
                        $minimum = $form->getValue('minimum');
                        $maximum = $form->getValue('maximum');
    			$edit = new Student_Model_DbTable_Scale();

                        //$valid = $form->getValues();
                       // print_r($valid);
                        $edit->editScale($id, $minimum, $maximum, $grade);
                        $this->_redirect('/student/scale/index/id/'.$id);
                }
                else
                {
                    $form->populate($formData);
                }
        }
        else
        {
            $id = $this->_getParam('id');
            if ($id > 0)
            {
                $scale = new Student_Model_DbTable_Scale();
                $form->populate($scale->getScale($id));
            }
        }

    }


    public function viewAction()
    {

       $id = $this->_getParam('id', 0);
       $scale = new Student_Model_DbTable_scale();
       $viewScale = $scale->getScaleDetails($id);

       //$course_id = $this->_getParam('id', 0);
      // $courses = new Student_Model_DbTable_Course();

       $this->view->viewScale = $viewScale;
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id');
        $this->view->title = "Delete this Scale";
        $this->view->headTitle($this->view->title);
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                //$id = $this->_getParam('id');
                $assessment = new Student_Model_DbTable_Scale();
                echo $id;
                $assessment->deleteScale($id);
                $this->_redirect('/student/scale/index/id/'.$id);
            }
            $this->_redirect('/student/scale/index/id/'.$id);
            } else {

                //
             $id = $this->_getParam('id');
             $scale = new Student_Model_DbTable_Scale();
             $delete = $scale->getScale($id);
             // print_r($delete);
            // print_r($id);
             $this->view->scale = $delete;
}
    }


}

